package clinic.ms.controller;

import clinic.ms.entity.Patient;
import clinic.ms.entity.Pie;
import clinic.ms.service.PatientServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class PatientController {

    @Autowired
    PatientServiceImpl patientService = new PatientServiceImpl();



    @PostMapping("/addPatient")
    public ModelAndView addPatient(@RequestParam(value = "patientName")String patientName,
                                   @RequestParam(value = "patientSex")String patientSex,
                                   @RequestParam(value = "patientBirthday",required = false)String patientBirthday,
                                   @RequestParam(value = "patientPhone",required = false)String patientPhone,
                                   @RequestParam(value = "patientAddress",required = false)String patientAddress,
                                   @RequestParam(value = "patientRemarks",required = false)String patientRemarks
                                   )
    {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String patientNo = format.format(currentTime);
        String patientCt = formatter.format(currentTime);
        Patient patient = new Patient(patientNo,patientName,patientSex,patientBirthday,patientPhone,patientAddress,patientRemarks,patientCt);
        patientService.insert(patient);
        List<Patient> patients = patientService.findAll();
        ModelAndView modelAndView = new ModelAndView("/addPatient");
        modelAndView.addObject("patients",patients);
        return modelAndView;
    }



    //编辑病人页面
    @GetMapping("/editPatient")
    public ModelAndView editPatient(@RequestParam("id") String patientNo)
    {
        Patient patient = patientService.queryPaByNo(patientNo);
        ModelAndView modelAndView = new ModelAndView("/editPatient");
        modelAndView.addObject("patient",patient);
        //将查询到的patient信息放进modelAndView里
        return modelAndView;
    }


    @PostMapping("editPatient")
    public ModelAndView edit(@RequestParam(value = "patientNo")String patientNo,
                                   @RequestParam(value = "patientName")String patientName,
                                   @RequestParam(value = "patientSex")String patientSex,
                                   @RequestParam(value = "patientBirthday",required = false)String patientBirthday,
                                   @RequestParam(value = "patientPhone",required = false)String patientPhone,
                                   @RequestParam(value = "patientAddress",required = false)String patientAddress,
                                   @RequestParam(value = "patientRemarks",required = false)String patientRemarks,
                                   @RequestParam(value = "patientCt",required = false)String patientCt)
    {
        Patient patient = new Patient(patientNo,patientName,patientSex,patientBirthday,patientPhone,patientAddress,patientRemarks,patientCt);
        System.out.println(patient);
        patientService.update(patient);
        List<Patient> patients=patientService.findAll();
        ModelAndView modelAndView = new ModelAndView("patientAdmin");
        modelAndView.addObject("patients",patients);
        return modelAndView;
    }


    @GetMapping("/patientAdmin")
    public ModelAndView findAll(){
        List<Patient> patients=patientService.findAll();
        ModelAndView modelAndView = new ModelAndView("patientAdmin");
        modelAndView.addObject("patients",patients);
        return modelAndView;
    }

    @PostMapping("/delPatient")
    public ModelAndView delete(String id)
    {
        System.out.println(id);
        patientService.delete(id);
        return new ModelAndView(new RedirectView("/patientAdmin"));
    }


    @GetMapping("/patientChart")
    public ModelAndView patientChart(){
        ModelAndView modelAndView = new ModelAndView("/patientChart");
        List<Pie> patientCount = patientService.queryPaByMonth("2019-08","2020-08");
        modelAndView.addObject("patientCount",patientCount);
        return modelAndView;
    }

    @PostMapping("/patientChart")
    public ModelAndView patientChart(@RequestParam("begin") String begin,
                                     @RequestParam("end") String end){
        ModelAndView modelAndView = new ModelAndView("/patientChart");
        List<Pie> patientCount = patientService.queryPaByMonth(begin,end);
        modelAndView.addObject("patientCount",patientCount);
        return modelAndView;
    }
}
